using System;
using System.Collections.Generic;
using System.Text;
using Barrage.Global;
using System.Xml;
using Barrage.Meshes.Text;
using Barrage.GUI.Elements;
using SlimDX;

namespace Barrage.GUI.Templates.Standard
{
    struct StandardGUITextAttributes
    {
        const string XML_WIDTH_ATTRIBUTE = "width";
        const string XML_HEIGHT_ATTRIBUTE = "height";
        const string XML_FONTSIZE_ATTRIBUTE = "fontSize";

        public Vector2 Size;
        public string BitmapFont;
        public float FontSize;

        const string DEFAULT_FONT = "Standard";
        const float DEFAULT_WIDTH = 1024;
        const float DEFAULT_HEIGHT = 768;
        const float DEFAULT_FONTSIZE = 24;


        public static StandardGUITextAttributes DefaultValues
        {
            get
            {
                return new StandardGUITextAttributes(
                    new Vector2(DEFAULT_WIDTH,DEFAULT_HEIGHT),
                    DEFAULT_FONT,
                    DEFAULT_FONTSIZE
                    );
            }
        }

        public StandardGUITextAttributes(Vector2 size, string BitmapFont, float FontSize)
        {
            this.Size = size;
            this.BitmapFont = BitmapFont;
            this.FontSize = FontSize;
        }
        public StandardGUITextAttributes(XmlNode xn)
        {
            Size = XmlHelper.ParseXMLVector2Attribute(xn, XML_WIDTH_ATTRIBUTE, XML_HEIGHT_ATTRIBUTE, new Vector2(DEFAULT_WIDTH, DEFAULT_HEIGHT));
            BitmapFont = DEFAULT_FONT;
            FontSize = XmlHelper.ParseXMLFloatAttribute(xn, XML_FONTSIZE_ATTRIBUTE, DEFAULT_FONTSIZE);
        }
    }
    /// <summary>
    /// Standard template GUIText Implementation
    /// </summary>
    public class StandardGUIText : GUIText
    {
        const string XML_DEFAULT_ATTRIBUTE = "default";
        StandardGUITextAttributes attributes;
        TextBoxMesh textMesh;
        
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="layout">Layout that will hold this item</param>
        /// <param name="xn">Xml Node that holds attribute values of this item</param>
        public StandardGUIText(GUILayout layout, XmlNode xn)
            : base(layout, GUITemplateTypes.Standard, xn)
        {
            processDefaultValues(xn);

            BitmapFont f = BitmapFontManager.Fonts[attributes.BitmapFont];
            float scale = attributes.FontSize / f.FontInfo.LineHeight;
            textMesh = new TextBoxMesh(f, new TextBoxDrawProperties(TextAlignment.Left, attributes.Size, scale, VerticalAlignment.Center, Vector2.Zero));
            textMesh.Text = Text;
            addMesh(textMesh);
        }

        void processDefaultValues(XmlNode xn)
        {
            bool loadDefaults = XmlHelper.GetXMLAttribute(xn, XML_DEFAULT_ATTRIBUTE).Equals("true");
            if (loadDefaults)
                attributes = StandardGUITextAttributes.DefaultValues;
            else
                attributes = new StandardGUITextAttributes(xn);

        }

        /// <summary>
        /// \internal Gets / Sets text label string
        /// </summary>
        public override string Text { set { textMesh.Text = value; base.Text = value; } }

        /// <summary>
        /// \internal Clear resources
        /// </summary>
        public override void Dispose()
        {
            textMesh = null;
            base.Dispose();
        }
    }
}
